package com.zby.zbyliving.commodity.controller;


import com.aliyun.oss.*;
import com.zby.common.utils.R;
import com.zby.zbyliving.commodity.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@Slf4j
@RestController
public class TestController {
    /*  @RequestMapping("/test")
      public R upload() throws FileNotFoundException {
          String endpoint = "oss-cn-nanjing.aliyuncs.com";
          String accessKeyId = "LTAI5tAX8PaqL42EBcehWrj9";
          String accessKeySecret = "El53LXBc9BQ1cdrfRWidlgbRJ05lWk";
          String bucketName = "zbyliving";
          String objectName = "be.jpg";
          String filePath = "E:\\be.jpg";
          OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
          try {
              InputStream inputStream = new FileInputStream(filePath);
              ossClient.putObject(bucketName, objectName, inputStream);
          } catch (OSSException oe) {
              System.out.println("Caught an OSSException, which means your request made it to OSS, "
                      + "but was rejected with an error response for some reason.");
              System.out.println("Error Message:" + oe.getErrorMessage());
              System.out.println("Error Code:" + oe.getErrorCode());
              System.out.println("Request ID:" + oe.getRequestId());
              System.out.println("Host ID:" + oe.getHostId());
          } catch (ClientException ce) {
              System.out.println("Caught an ClientException, which means the client encountered "
                      + "a serious internal problem while trying to communicate with OSS, "
                      + "such as not being able to access the network.");
              System.out.println("Error Message:" + ce.getMessage());
          } finally {
              if (ossClient != null) {
                  ossClient.shutdown();
              }
          }
          return null;
      }*/
    @Resource
    private OSSClient ossClient;

    @RequestMapping("/test2")
    public R testUpload2() throws FileNotFoundException {
        log.info("test Upload2");
        InputStream inputStream =
                new FileInputStream("E:\\be.jpg");
        ossClient.putObject("zbyliving", "2.jpg", inputStream);
        ossClient.shutdown();
        System.out.println(" 上传 ok~");
        return null;
    }
    @Resource
    CategoryService categoryService;
    @RequestMapping("/test3")
    public  R testGeT(){
        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(301L);
        return R.ok().put("data", cascadedCategoryId);
    }
}
